;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.ThreeStatisticsReport", [])
        .controller("ThreeStatisticsReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false];

            $scope.condition = {};
            $scope.dataList = [];

            $scope.switchDatePicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };
            $scope.sendMethodList = [];

            $scope.tbConf = {
                "scrollWidth": 1500,
                "tableCol": [
                    {
                        "type": "",
                        "name": "序号",
                        "dataField": "1",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "月份",
                        "dataField": "2",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "服务人员",
                        "dataField": "3",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "CRM单号",
                        "dataField": "4",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "三包申请单号",
                        "dataField": "5",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "服务类型",
                        "dataField": "6",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "申请时间",
                        "dataField": "7",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "审批状态",
                        "dataField": "8",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "发货方式",
                        "dataField": "9",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "发运单号",
                        "dataField": "10",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "物流状态",
                        "dataField": "11",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "发货日期",
                        "dataField": "12",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "种类",
                        "dataField": "13",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "数量",
                        "dataField": "14",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "收货地址",
                        "dataField": "15",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "快递单号",
                        "dataField": "16",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    },
                    {
                        "type": "",
                        "name": "备注",
                        "dataField": "17",
                        "width": 180,
                        "isShow": true,
                        "isFixed": false,
                        "toSort": false,
                        "sortType": ""
                    }
                ]
            };

            $scope.sapStatusEnum = [{
                    key: 2,
                    value: "已过账"
                },
                {
                    key: 1,
                    value: "部分过账"
                },
                {
                    key: 0,
                    value: "未过账"
                }
            ];


            $scope.yesOrNo = [{
                    key: 1,
                    value: "是"
                },
                {
                    key: 0,
                    value: "否"
                }
            ];

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("dReport", "/report/threeguarantees/deliveryTongjiNj/export")
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            var btnPermissionCode = {
                threeStatisticsReportExport: window.ActionCode.threeStatisticsReportExport,
            };

            $scope.btnPermission = {
                threeStatisticsReportExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var service = "dReport";
                var reqUrl = "/report/threeguarantees/deliveryTongjiNj";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    angular.forEach($scope.dataList, function (item) {
                        item.deliveryDateShow = $filter('YbDate')(item.deliveryDate, "yyyy-MM-dd HH:mm:ss");
                        item.applicationDateShow = $filter('YbDate')(item.applicationDate, "yyyy-MM-dd HH:mm:ss");

                    });
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };
            // 查询运输方式
            function getSendMethods () {
                if ($scope.sendMethodList.length > 0) {
                    return;
                }
                var service = "device";
                var reqUrl = "/sendMethodDictionary/list";
                var param = {pId: 0};
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.sendMethodList = resp.value;
                })
            }


            (function () {
                //getDataList();
                getSendMethods();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);
